Ext.define('PJ.view.workmanagement.List', {
    extend: 'AM.default.DefaultList',
    alias: ['widget.workmanagementList'],
    requires: ['AM.default.DefaultSearchForm',
               'AM.default.DefaultDateField',
               'AM.default.jsonp.DefaultLoadJsonp'],
               
    initComponent: function(){
        var me = this;
        me.columns = [{
            header: '姓名',
            dataIndex: 't.userName',
            flex: 1,
            minWidth: 90
        }, {
            header: '登录号',
            dataIndex: 't.loginId',
            flex: 1,
            minWidth: 100
        }, {
            header: '管辖单位',
            dataIndex: 't.jurisdictions',
            flex: 1,
            minWidth: 100,
            renderer: function(value, meta, record){
                meta.tdAttr = "data-qtip='" + value + "'";
                return value;
            }
        }, {
            header: '创建时间',
            dataIndex: 't.created',
            flex: 1,
            minWidth: 100,
            renderer: function(value, meta, record){
                meta.tdAttr = "data-qtip='" + value + "'";
                return value;
            }
        }, {
            header: '启用状态',
            dataIndex: 't.enabled',
            flex: 1,
            minWidth: 70,
            renderer: function(value, meta, record){
                if(value == 1){
                	return "启用";
                }else if(value == 2){
                	return "作废";
                }
            }
        }];
        me.store = Ext.create('PJ.'+ prototype +'.workmanagement.List', {
            isLoad: false
        });
        me.callParent(arguments);
        me.loadType =  gj_onlineLearning_constant.loadType;
    },
    
    //新增
    onAddClick: function(button){
    	var me = this;
    	var parentID = me.searchCategoryIndex;
        if (parentID == null) {
            Ext.Msg.alert(resource_lang.messageAlertTitle_warning, resource_lang.addInfo.serialnoMsg);
        }else {
        	Ext.create('PJ.view.workmanagement.Edit', {
                title: "新增",
                width: 330,
                grid:me,
                isView: false,
                isReadOnly: false,
                isEdit: false,
                classId: parentID
        	});
        }
    },
    
    //启用
    onEnableClick:function(){
    	var me=this;
    	var store=me.getStore();
    	var records=me.getSelectionModel().getSelection();
    	Ext.Msg.confirm({
    		title:'提示',
    		msg:'您确定要启用这些数据吗?',
    		icon:Ext.Msg.WARNING,
    		buttons:Ext.Msg.OKCANCEL,
    		fn:function(id){
    			if(id=='ok'){
    				var userIds= new Array();
    				Ext.Array.each(records,function(name,index){
    					userIds.push(name.get('userId'));
    				});
    				if(gj_onlineLearning_constant.loadType == "jsonp"){
    					var store1 = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
    						loadType: gj_onlineLearning_constant.loadType,
    						url: store.enableUrl,
    						extraParams: {
    							userIds: userIds
    						}
    					});
    					store1.load(function(rec, operation, success){
    						if(success){
    							Ext.Msg.alert('消息', '启用成功！', function(){
    								var store = me.getStore();
    								store.load();
    							});
    						}else{
    							Ext.Msg.alert('消息', operation.error);
    						}
    					});
    				}else{
    					Ext.Ajax.request({
    						url:store.enableUrl,
    						params: {
    							userIds: userIds
    						},
    						success:function(response){
    							var data=response.responseText;
    							var json=Ext.decode(data,true);
    							if(json.success){
    								Orchid.alert('提示','启用成功!',function(){
    									store.load();
    								});
    							}else{
    								Orchid.alert('提示',json.msg);
    							}
    						}
    					});
    				}
    			}
    		}
    	});
    },
    /**
     * 作废
     */
    onInvalidClick: function(){
    	var me=this;
    	var store=me.getStore();
    	var records=me.getSelectionModel().getSelection();
    	Ext.Msg.confirm({
    		title:'提示',
    		msg:'您确定要作废这些数据吗?',
    		icon:Ext.Msg.WARNING,
    		buttons:Ext.Msg.OKCANCEL,
    		fn:function(id){
    			if(id=='ok'){
    				var userIds= new Array();
    				Ext.Array.each(records,function(name,index){
    					userIds.push(name.get('userId'));
    				});
    				if(gj_onlineLearning_constant.loadType == "jsonp"){
    					var store1 = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
    						loadType: gj_onlineLearning_constant.loadType,
    						url: store.invalidUrl,
    						extraParams: {
    							userIds: userIds
    						}
    					});
    					store1.load(function(rec, operation, success){
    						if(success){
    							Ext.Msg.alert('消息', '作废成功！', function(){
    								var store = me.getStore();
    								store.load();
    							});
    						}else{
    							Ext.Msg.alert('消息', operation.error);
    						}
    					});
    				}else{
    					Ext.Ajax.request({
    						url:store.invalidUrl,
    						params: {
    							userIds: userIds
    						},
    						success:function(response){
    							var data=response.responseText;
    							var json=Ext.decode(data,true);
    							if(json.success){
    								Orchid.alert('提示','作废成功!',function(){
    									store.load();
    								});
    							}else{
    								Orchid.alert('提示',json.msg);
    							}
    						}
    					});
    				}
    			}
    		}
    	});
    },
    /**
     * 删除
     */
    onDeleteClick: function(){
    	var me=this;
    	var store=me.getStore();
    	var records=me.getSelectionModel().getSelection();
    	Ext.Msg.confirm({
    		title:'提示',
    		msg:'确认删除所选择的管理员吗？',
    		icon:Ext.Msg.WARNING,
    		buttons:Ext.Msg.OKCANCEL,
    		fn:function(id){
    			if(id=='ok'){
    				var userIds= new Array();
    				Ext.Array.each(records,function(name,index){
    					userIds.push(name.get('userId'));
    				});
    				if(gj_onlineLearning_constant.loadType == "jsonp"){
    					var store1 = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
    						loadType: gj_onlineLearning_constant.loadType,
    						url: store.delUrl,
    						extraParams: {
    							userIds: userIds
    						}
    					});
    					store1.load(function(rec, operation, success){
    						if(success){
    							Ext.Msg.alert('消息', '删除成功！', function(){
    								var store = me.getStore();
    								store.load();
    							});
    						}else{
    							Ext.Msg.alert('消息', operation.error);
    						}
    					});
    				}else{
    					Ext.Ajax.request({
    						url:store.delUrl,
    						params: {
    							userIds: userIds
    						},
    						success:function(response){
    							var data=response.responseText;
    							var json=Ext.decode(data,true);
    							if(json.success){
    								Orchid.alert('提示','删除成功!',function(){
    									store.load();
    								});
    							}else{
    								Orchid.alert('提示',json.msg);
    							}
    						}
    					});
    				}
    			}
    		}
    	});
    },
    /**
     * 重置密码
     */
    onResetPasswordClick: function(){
    	var me=this;
    	var store=me.getStore();
    	var records=me.getSelectionModel().getSelection();
    	Ext.Msg.confirm({
    		title:'提示',
    		msg:'确认重置所选择的管理员的密码吗？（重置后默认密码为：8888）',
    		icon:Ext.Msg.WARNING,
    		buttons:Ext.Msg.OKCANCEL,
    		fn:function(id){
    			if(id=='ok'){
    				var userIds= new Array();
    				Ext.Array.each(records,function(name,index){
    					userIds.push(name.get('userId'));
    				});
    				if(gj_onlineLearning_constant.loadType == "jsonp"){
    					var store1 = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
    						loadType: gj_onlineLearning_constant.loadType,
    						url: store.resetPasswordUrl,
    						extraParams: {
    							userIds: userIds
    						}
    					});
    					store1.load(function(rec, operation, success){
    						if(success){
    							Ext.Msg.alert('消息', '密码重置成功！', function(){
    								var store = me.getStore();
    								store.load();
    							});
    						}else{
    							Ext.Msg.alert('消息', operation.error);
    						}
    					});
    				}else{
    					Ext.Ajax.request({
    						url:store.resetPasswordUrl,
    						params: {
    							userIds: userIds
    						},
    						success:function(response){
    							var data=response.responseText;
    							var json=Ext.decode(data,true);
    							if(json.success){
    								Orchid.alert('提示','密码重置成功!',function(){
    									store.load();
    								});
    							}else{
    								Orchid.alert('提示',json.msg);
    							}
    						}
    					});
    				}
    			}
    		}
    	});
    },
    listeners: {
    	selectionchange: function(selModel, selected){
            this.down('#' + this.oldId + "Enable").setDisabled(selected.length === 0);//启用
            this.down('#' + this.oldId + "Invalid").setDisabled(selected.length === 0);//作废
            this.down('#' + this.oldId + "ResetPassword").setDisabled(selected.length === 0);//重置密码
    	},
        viewready: function(){
            this.addDocked([{
                dock: 'top',
                xtype: "searchform",
                columnCount: 3,
                fieldDefaults: {
                    labelAlign: 'right',
                    labelWidth: 60,
                    maxLength: 250
                },
                items: [{
                    xtype: "textfield",
                    fieldLabel: '姓名',
                    name: "searchBookName",
                    emptyText: "请输入姓名"
                },{
                    xtype: "textfield",
                    fieldLabel: '登录号',
                    name: "searchAuthor",
                    emptyText: "请输入登录号"
                }],
                onResetClick: function(button){
                    var form = button.up('form');
                    var gridPanel = form.grid;
                    var searchCategoryIndex = gridPanel.searchCategoryIndex;
                    var newParams = {
                    		searchTreePath: searchCategoryIndex
                    };
                    gridPanel.getStore().proxy.extraParams = {};
                    
                    // 4.0强制更改store参数写法
                    var params = gridPanel.getStore().proxy.extraParams;
                    Ext.apply(params, newParams);
                    
                    form.getForm().reset();
                },
                grid: this
            },{
            	xtype: 'toolbar',
            	dock: 'top',
            	displayInfo: true,
            	items: [this.addButton,
            	"-", {
            		text: '启用',
            		iconCls: 'reuseIco',
            		itemId: this.oldId + "Enable",
            		disabled: true,
            		scope: this,
            		handler: this.onEnableClick
            	},"-", {
            		text: '作废',
            		iconCls: 'discardIco',
            		itemId: this.oldId + "Invalid",
            		disabled: true,
            		scope: this,
            		handler: this.onInvalidClick
            	},"-",this.deleteButton,"-", {
            		text: '重置密码',
            		iconCls: 'refurbishIco',
            		itemId: this.oldId + "ResetPassword",
            		disabled: true,
            		scope: this,
            		handler: this.onResetPasswordClick
            	},"->", "->", this.countSelectPanel]
            }]);
            // 触发权限过滤事件
            this.fireEvent("permission", this);
        }
    }
});
